/*
 * Copyright (C) 2014 luke.
 * 
 * This file is part of Silvie.
 * 
 * Silvie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Silvie is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Silvie.  If not, see <http://www.gnu.org/licenses/>.
 */

package utils;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Image;

/**
 * Utility class simplifying the creation of double-buffered canvases.
 * 
 * @author luke
 * @see <a
 *      href="http://www.realapplets.com/tutorial/DoubleBuffering.html">Double-Buffering</a>
 */
public abstract class DoubleBufferedCanvas extends Canvas {
	private static final long serialVersionUID = 1L;

	@Override
	public void paint(Graphics g) {
		Image image = getOffscreenImage();
		if (image != null) {
			g.drawImage(image, 0, 0, null);
		}
	}

	@Override
	public void update(Graphics g) {
		paint(g);
	}

	/**
	 * Returns the "offscreen" image. This is a requirement for
	 * double-buffering.
	 * 
	 * @return the offscreen image.
	 */
	public abstract Image getOffscreenImage();
}
